﻿using System;
using System.Resources;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using System.Text;
using Portal.Environment;
using Portal.Interface;
using Portal.Language;

namespace Portal.CMS
{
    public class GroupLink : Portal.Interface.WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "GroupLink";
            }
        }

       

        public override bool IsMainWebPart
        {
            get
            {
                return true;
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Title","Nhóm liên kết");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId,"Description","");
            }
        }

        public override Portal.Interface.WebPartInt[] SubWebParts
        {
            get
            {
                Portal.Interface.WebPartInt[] SubWebParts = new Portal.Interface.WebPartInt[]{
                    new AddGroupLink(),new UpdateGroupLink()
                };
                return SubWebParts;
            }
        }

       

        [AjaxPro.AjaxMethod]
        public string GetHtml(string belongSite)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                Environments.CheckWebPartAllow(WebPartId, true);

                string SelectSite =
                    "<select id=drpSelectSiteForGroupLink onchange=javascript:FilterFunc();>";
                string[] Sites = Environments.GetSites();
                for (int iIndex = 0; iIndex < Sites.Length; iIndex++)
                {
                    if (Sites[iIndex].ToLower().Equals(belongSite))
                    {
                        SelectSite += "<option selected value=\"" + Sites[iIndex] + "\">" + Sites[iIndex] + "</option>";
                    }
                    else
                    {
                        SelectSite += "<option value=\"" + Sites[iIndex] + "\">" + Sites[iIndex] + "</option>";
                    }
                }
                SelectSite += "</select>";
                DataTable dtGroupLink = new GroupLinkBLL().Reading(belongSite);
                string RetHtml =
                    "   <div>" + ResourceBLL.GetText(WebPartId, "SelectSite", "Site") + ":" + SelectSite + "&nbsp;<input type=button value=\"" + ResourceBLL.GetText(WebPartId, "Go", "Lọc dữ liệu") + "\" onclick=javascript:FilterFunc();>&nbsp;<input type=button onclick=\"javascript:ClientAddGroupLink();\" value=\"" + ResourceBLL.GetText(WebPartId, "TH_Add", "Thêm") + "\"></div>" +
                    "   <div id=divProcessing></div>";
                if (dtGroupLink.Rows.Count == 0)
                {
                    RetHtml += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
                }
                else
                {
                    RetHtml +=
                    "   <table>" +
                    "       <tr>" +
                    "           <th>" + ResourceBLL.GetText(WebPartId, "TH_Subject", "Tiêu đề") + "</th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th>" + ResourceBLL.GetText(WebPartId, "Owner", "Người tạo") + "</th>" +
                    "           <th></th>" +
                    "       </tr>";


                    for (int iIndex = 0; iIndex < dtGroupLink.Rows.Count; iIndex++)
                    {
                        string GroupLinkId = (string)dtGroupLink.Rows[iIndex]["GroupLinkId"];
                        string Subject = (string)dtGroupLink.Rows[iIndex]["Subject"];
                        string Owner = (string)dtGroupLink.Rows[iIndex]["Owner"];
                        string DeleteUrl = "javascript:ClientDeleteGroupLink('" + GroupLinkId + "')";
                        string PermissionUrl = "javascript:ShowPermissionFuncForm('" + GroupLinkId + "')";
                        string SyncUrl = "javascript:ClientSyncWithServer('" + GroupLinkId + "')";
                        RetHtml +=
                        "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                        "           <td>" + Subject + "</td>" +
                        "           <td><a href=\"javascript:ShowClientUpdateUrlForm('" + GroupLinkId + "');\">" + ResourceBLL.GetText(WebPartId, "TH_Update", "Cập nhật") + "</a></td>" +
                        "           <td><a href=\"" + DeleteUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Delete", "Xóa") + "</a></td>" +
                        "           <td><a href=\"" + PermissionUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Permission", "Phân quyền") + "</a></td>" +
                        "           <td><a href=\"" + SyncUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Sync", "Đồng bộ") + "</a></td>" +
                        "           <td>" + Owner + "</td>" +
                        "           <td></td>" +
                        "       </tr>";
                    }
                    RetHtml +=
                        "   </table>";
                }
                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(GroupLink));
            string ActiveSite = Portal.Environment.Environments.GetActiveSite();
            string RetHtml =
                Portal.Environment.WebPartUtilities.AttachJavascript("Portal.CMS", "GroupLink.js") +
                "<div id=divGroupLinkMain class=cssDivGroupLink>" + GetHtml(ActiveSite) + "</div>";
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string GetAddGroupLinkUrl(string belongSite)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("GroupLinks");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddGroupLinks", WebPartId, null, true);
        

                return Portal.Environment.Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new AddGroupLink().WebPartId),
                        new ParamCls("belongSite",belongSite)
            });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg()
        {
            return ResourceBLL.GetText(WebPartId,"ConfirmDeleteMsg","Bạn đã chắc chắn xóa chưa?");
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideCheckAllowDelete(string GroupLinkId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("GroupLinkItems");
                GroupLinkCls GroupLinkCls = new GroupLinkBLL().CreateModel(GroupLinkId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteGroupLinkItem", GroupLinkId, GroupLinkCls.Owner, true);
                
                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteGroupLink(string GroupLinkId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                ServerSideCheckAllowDelete(GroupLinkId);
                //PermissionTypeCls PermissionTypeCls = FindPermissionType("GroupLinkItems");
                //GroupLinkCls GroupLinkCls = new GroupLinkBLL().CreateModel(GroupLinkId);
                //Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteGroupLinkItem", GroupLinkId, GroupLinkCls.Owner, true);
                SqlTransaction sqlTransaction = Environment.Environments.CreateTransaction();
                try
                {
                    new GroupLinkBLL().DeleteGroupLink(sqlTransaction, GroupLinkId);
                    sqlTransaction.Commit();
                }
                catch (Exception ex)
                {
                    sqlTransaction.Rollback();
                    throw (ex);
                }
                return ResourceBLL.GetText(WebPartId,"DeleteOkMessage","Xóa thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetShowUpdateForm(string GroupLinkId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);

                PermissionTypeCls PermissionTypeCls=FindPermissionType("GroupLinkItems");
                GroupLinkCls GroupLinkCls = new GroupLinkBLL().CreateModel(GroupLinkId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateGroupLinkItem", GroupLinkId, GroupLinkCls.Owner, true);

                string UpdateUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UpdateGroupLink().WebPartId),
                        new ParamCls("GroupLinkId",GroupLinkId)
                    });
                return UpdateUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        public override Portal.Interface.FunctionCls[] FunctionList
        {
            get
            {
                return new FunctionCls[]
                {
                    new FunctionCls("ViewGroupLinks",ResourceBLL.GetText(WebPartId,"ViewGroupLinks","Xem nhóm liên kết"),WebPartId),
                    new FunctionCls("AddGroupLinks",ResourceBLL.GetText(WebPartId,"AddGroupLinks","Thêm mới nhóm liên kết"),WebPartId),
                    new FunctionCls("UpdateGroupLinkItem",ResourceBLL.GetText(WebPartId,"UpdateGroupLinkItem","Cập nhật chi tiết"),WebPartId),
                    new FunctionCls("DeleteGroupLinkItem",ResourceBLL.GetText(WebPartId,"DeleteGroupLinkItem","Xóa chi tiết"),WebPartId),
                    new FunctionCls("PermissionGroupLinks",ResourceBLL.GetText(WebPartId,"PermissionGroupLink","Phân quyền nhóm liên kết"),WebPartId),
                    new FunctionCls("PermissionGroupLinkItem",ResourceBLL.GetText(WebPartId,"PermissionGroupLinkItem","Phân quyền cho từng bản ghi nhóm liên kết"),WebPartId),
                };
            }
        }

        public override Portal.Interface.PermissionTypeCls[] PermissionTypes
        {
            get
            {
                return new PermissionTypeCls[]{
                    new PermissionTypeCls("GroupLinkItems","Liên kết website (Items)",new string[]{"PermissionGroupLinkItem","UpdateGroupLinkItem","DeleteGroupLinkItem"}),
                    new PermissionTypeCls("GroupLinks","Liên kết website",new string[]{"AddGroupLinks","PermissionGroupLinks"})
                };
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetShowFormSetPermission(string GroupLinkId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("GroupLinkItems");
                GroupLinkCls GroupLinkCls = new GroupLinkBLL().CreateModel(GroupLinkId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "PermissionGroupLinkItem", GroupLinkId, GroupLinkCls.Owner, true);
                return Environment.Permission.GetPermissionUrl(WebPartId, GroupLinkId, "GroupLinkItems");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        public override PermissionTypeCls GetServicePermissionType()
        {
            return PermissionTypes[1];
        }


        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
                return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang đọc dữ liệu...") + "</a>";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideSyncWithServer(string GroupLinkId)
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                new GroupLinkBLL().SyncWithServer(GroupLinkId);
                return GetText("SyncOk", "Đồng bộ với máy chủ web thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetConfirmSyncAllMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmSyncAllMsg", "Bạn đã chắc chắn update lại dữ liệu tin tức trên máy chủ từ máy local chưa?");
        }
    }
}